/*
 * CloudBank Mobile Knowledge Sharing for Language Learners
 * Supported by the JISC Rapid Innovation Programme #jiscri
 *
 * Copyright (C) 2010 University of Brighton
 *
 * Licensed under the GNU General Public License v3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package uk.ac.brighton.itrg.cloudbank;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class ActivitySettings extends PreferenceActivity 
                              implements Preference.OnPreferenceChangeListener
{
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		
		// for all text preferences, set summary to current value  
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		for(String key : preferences.getAll().keySet()) 
		{  
		    Preference pref = findPreference(key);  
		    if(pref == null) continue;  
		    if(pref instanceof EditTextPreference) 
	    	{
		    	// init summary with current value
				pref.setSummary(tagSummary(((EditTextPreference)pref).getText())); 

				// register listener to update summary when values change 
				pref.setOnPreferenceChangeListener(this); 
	    	}
		}  
	}

	public boolean onPreferenceChange(Preference pref, Object objValue) 
	{ 
		String key = pref.getKey();
		String val = (String) objValue;
		
		     if(key.equals(getString(R.string.setting_tags_include_key))) CB.INCLUDE_TAGS = val;
		else if(key.equals(getString(R.string.setting_tags_exclude_key))) CB.EXCLUDE_TAGS = val;
		else if(key.equals(getString(R.string.setting_tags_default_key))) CB.DEFAULT_TAGS = val; 

    	pref.setSummary(tagSummary(val)); // update settings display
		
		return true; 
	}
	
	private String tagSummary(String s)
	{
		if((s != null) && (s.length() > 0)) return s; 
		else return getString(R.string.settings_tags_none);
	}
}
